package com.hos.db.create.ill;

import com.hos.db.entity.ill.IllCategoryValue;
import com.hos.db.entity.ill.IllOptionEntity;
import com.hos.utile.DLog;
import com.hos.utile.JsonUtile;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * 病情填写选择
 * Created by guom on 2018/10/15.
 */
public class IllOptionTable {
    public static String tableName = "ill_option";
    public static String tableCategoryId = "category_id";//类别id
    public static String tableCategoryName = "category_name";//类别名称
    public static String tableCategoryValue = "category_value";//出院建议

    //
    public static String careteTable = "create table  "
            + tableName + " ( "
            + tableCategoryId + "  int(200), "
            + tableCategoryName + " longText, "
            + tableCategoryValue + " longText "
            + "); ";

    //
    public static List<String[]> getOptions() {
        String[] v1 = new String[]{"1", "部位", "头部、颈部、胸部、腹部、背部、左上肢、右上肢、左下肢、右下肢、补充说明"};
        String[] v2 = new String[]{"2", "症状", "疼痛、肿胀、寒冷、发热、沉重感、麻木感、眩晕、血管曲张、跛行、溃疡、坏疽、体检发现、补充说明"};
        String[] v3 = new String[]{"3", "合并症", "高血压、高血脂、糖尿病、冠心病、房颤、脑梗塞、脑出血、肾病、肿瘤、家族疾病、补充说明"};
        String[] v4 = new String[]{"4", "神志", "清楚、不清楚、补充说明"};
        String[] v5 = new String[]{"5", "头部阳性特征", "有、无"};
        String[] v6 = new String[]{"6", "颈部", "搏动性肿块、静脉扩张、补充说明"};
        String[] v7 = new String[]{"7", "胸部", "心率不齐、听诊杂音、胸水征、啰音、哮鸣音、静脉扩张、补充说明"};
        String[] v8 = new String[]{"8", "腹部", "搏动性肿块、腹壁静脉曲张、压痛、反跳痛、腹水征、血管杂音、补充说明"};
        String[] v9 = new String[]{"9", "腹股沟区位置", "左、右、双侧"};
        String[] v10 = new String[]{"10", "腹股沟区症状", "搏动性肿块、股动脉搏动、静脉曲张、血管杂音、补充说明"};
        String[] v11 = new String[]{"11", "上肢位置", "左、右、双侧"};
        String[] v12 = new String[]{"12", "上肢症状", "压痛、肿胀、静脉扩张、肱动脉搏动、桡动脉搏动、溃疡坏疽、皮温增高、皮温降低、血管杂音、补充说明"};
        String[] v13 = new String[]{"13", "下肢位置", "左、右、双侧"};
        String[] v14 = new String[]{"14", "下肢症状", "压痛、肿胀、静脉曲张、腘动脉搏动、足背动脉搏动、胫后动脉搏动、溃疡坏疽、皮温增高、皮温降低、血管杂音、补充说明"};
        String[] v15 = new String[]{"15", "神经系统阳性体征", "有、无"};
        String[] v16 = new String[]{"16", "手术侧别", "左、右、双侧、无"};
        String[] v17 = new String[]{"17", "保守治疗", "抗凝、抗血小板、溶栓、抗感染、控压、控制糖尿病、压力治疗、卧床休息、抬高患肢、戒烟、补充说明"};
        String[] v18 = new String[]{"18", "治疗结果", "痊愈、好转、未愈、转院、补充说明"};

        List<String[]> options = new ArrayList();
        options.add(v1);
        options.add(v2);
        options.add(v3);
        options.add(v4);
        options.add(v5);
        options.add(v6);
        options.add(v7);
        options.add(v8);
        options.add(v9);
        options.add(v10);
        options.add(v11);
        options.add(v12);
        options.add(v13);
        options.add(v14);
        options.add(v15);
        options.add(v16);
        options.add(v17);
        options.add(v18);
        //options = getOptions(options);
        return options;
    }

    private static List<String[]> getOptions(List<String[]> list) {
        List<String[]> datas = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            String[] d = getOptions(list.get(i));
            datas.add(d);
        }
        return datas;
    }

    private static String[] getOptions(String[] value) {
        List<IllCategoryValue> datas = new ArrayList();
        String[] values = value[2].split("、");
        for (int j = 0; j < values.length; j++) {
            IllCategoryValue v = new IllCategoryValue();
            v.setCategoryId(value[0]);
            v.setId(value[0] + "H" + String.valueOf(j + 1));
            v.setValue(values[j]);
            v.setExplain("补充说明".equals(values[j]));
            datas.add(v);
        }
        String json = JsonUtile.obj2Json(datas);
        value[2] = json;
        return value;
    }

    public static List<IllOptionEntity> getOptions(JdbcTemplate jdbcTemplate, String[] keys) {
        String where = "";
        if (keys != null) {
            for (int i = 0; i < keys.length; i++) {
                if (i == 0) {
                    where = " WHERE " + IllOptionTable.tableCategoryId + " = '" + keys[i] + "'";
                    continue;
                }
                where += " OR " + IllOptionTable.tableCategoryId + " = '" + keys[i] + "'";
            }
        }
        String sql = "SELECT * FROM " + IllOptionTable.tableName
                + where
                + " ORDER BY "
                + IllOptionTable.tableCategoryId
                + " ASC ";
        List<IllOptionEntity> options = onQuery(jdbcTemplate, sql);
        if (options == null) {
            options = new ArrayList();
        }
        return options;
    }


    private static List<IllOptionEntity> onQuery(JdbcTemplate jdbcTemplate, String sql) {
        List<IllOptionEntity> ills = null;
        try {
            ills = jdbcTemplate.query(sql,
                    ParameterizedBeanPropertyRowMapper.newInstance(IllOptionEntity.class));
            DLog.e("查询成功：==========");
        } catch (Exception e) {
            DLog.e("查询失败：" + e.getMessage());
        }
        return ills;
    }
}
